#include "utils/std.h"
#include "utils/file.h"
#include "utils/dir.h"
#include "cmd.h"
#include "config.h"
#include "tree.h"
#include "oid.h"
#include "index.h"

tree_t *index_load()
{
    tree_t *this = tree_read(GIT_INDEX_PATH);
    return this;
}

void index_store(tree_t *this)
{
    tree_write(this, GIT_INDEX_PATH);
}

void index_dup(oid_t *oid)
{
    get_file_oid(GIT_INDEX_PATH, oid);

    char target_path[PATH_MAX];
    get_file_oid(oid, target_path);

    copy_file(GIT_INDEX_PATH, target_path);
}

void git_add(int argc, char *argv[])
{
    if(argc < 2){
        fprintf(stderr,"Usage:git add <file>...\n");
        return;
    }
    tree_t *index = index_load();
    if(index == NULL){
        fprintf(stderr,"Falied to load index.\n");
        return;
    }
    for(int i=1;i<argc;i++){
        const char *filepath = argv[i];
        if(is_directory(filepath)){
            fprintf(stderr,"warning");
            continue;
        }
        add_file_to_index(filepath,index);
    }
    index_store(index);
}

void git_rm(int argc, char *argv[])
{
    if(argc<2){
        return;
    }
    tree_t *index = index_load();
    if(index == NULL){
        return;
    }
    for(int i=1;i<argc;i++){
        const char *filepath = argv[i];
        if(is_directory(filepath)){
            continue;
        }
    }
    index_store(index);
}
